package com.qyhl.module_activities.act.signup;

import android.util.Log;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;
import com.qyhl.webtv.commonlib.entity.act.ActivityViewBean;
import com.qyhl.webtv.commonlib.entity.act.ActivityViewOptionBean;
import com.qyhl.webtv.commonlib.entity.act.PlayerResultBean;
import com.qyhl.webtv.commonlib.entity.act.PlayerResultWorkBean;
import com.qyhl.webtv.commonlib.entity.act.UploadBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by qyhl2 on 2017/12/25.
 */

public class SignUpActPresenter implements SignUpActContract.SignUpActPresenter {

    private final SignUpActContract.SignUpActView view;
    private final SignUpActModel model;

    /**
     * gson解析
     */
    Gson gson = new Gson();

    SignUpActPresenter(SignUpActContract.SignUpActView view) {
        this.view = view;
        this.model = new SignUpActModel(this);
    }

    @Override
    public void onError(int i, String string) {
        view.onError(string);
    }

    @Override
    public void onSuccess(List<ActivityViewBean> data) {
        if (data != null && data.size() == 0) {
            view.onEmpty();
        } else {
            view.onSuccess(data);
        }
    }

    @Override
    public void getActViews(int mActId) {
        model.getActView(mActId);
    }

    /**
     * 获取多选String
     *
     * @param checks
     * @return
     */
    @Override
    public String getOptionStr(List<String> checks) {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < checks.size(); i++) {
            str.append(checks.get(i) + ",");
        }
        return str.toString();
    }

    /**
     * 获取多选已选Id
     *
     * @param checks
     * @param viewOptions
     * @return
     */
    @Override
    public String getOptioinIds(List<String> checks, List<ActivityViewOptionBean> viewOptions) {
        StringBuffer str = new StringBuffer();
        for (ActivityViewOptionBean option : viewOptions) {
            for (String s : checks) {
                if (s.equals(option.getOptionName())) {
                    str.append(option.getId() + ",");
                }
            }
        }
        return str.toString();
    }

    @Override
    public void getTokenSuccess(UpTokenBean data, boolean isUpload) {
        view.getTokenSuccess(data, isUpload);
    }

    @Override
    public void getTokenError(boolean isUpload) {
        view.getTokenError(isUpload);
    }


    @Override
    public void getToken(boolean isUpload) {
        model.getQiuniuToken(isUpload);
    }

    @Override
    public void combinationData(List<PlayerResultBean> results, List<UploadBean> files, int actId, String workName, String workDescription, String username, String videoWorkName,String videoDesc) {
        HashMap<String, String> params = new HashMap<>();

        List<PlayerResultWorkBean> workBeans = new ArrayList<>();
        //视频选择返回
        PlayerResultWorkBean videoResultWorkBean = new PlayerResultWorkBean();
        for (int i = 0; i < files.size(); i++) {
            UploadBean uploadBean = files.get(i);
            switch (uploadBean.getType()) {
                case 1:
                    //自定义view
                    PlayerResultBean playerResultBean = new PlayerResultBean();
                    playerResultBean.setResultText(uploadBean.getUrl());
                    playerResultBean.setType("image");
                    playerResultBean.setViewId(uploadBean.getViewId());
                    playerResultBean.setViewText(uploadBean.getViewText());
                    //报名图
                    if (view.getApplyViewId() == uploadBean.getViewId()) {
                        playerResultBean.setIsShow(2);
                    }
                    results.add(playerResultBean);
                    break;
                case 2:
                    //作品
                    PlayerResultWorkBean playerResultWorkBean = new PlayerResultWorkBean();
                    playerResultWorkBean.setUrl(uploadBean.getUrl());
                    playerResultWorkBean.setType("0");
                    workBeans.add(playerResultWorkBean);
                    break;
                case 3:
                    //视频
                    videoResultWorkBean.setType("1");
                    videoResultWorkBean.setUrl(uploadBean.getUrl());
                    break;
                case 4:
                    videoResultWorkBean.setVideoCover(uploadBean.getUrl());
                    //视频封面
                    break;
                case 5:
                    //报名图
                    params.put("applyUrl", uploadBean.getUrl());
                    break;
                default:
                    break;

            }
        }
        //视频作品
        if (!StringUtils.isEmpty(videoResultWorkBean.getUrl())) {
            workBeans.add(videoResultWorkBean);
        }
        String headicon = CommonUtils.getInstance().getUserLogo();
        params.put("playerWorkString", gson.toJson(workBeans));
        params.put("playerViewString", gson.toJson(results));
        params.put("actId", actId + "");
        params.put("siteId", CommonUtils.getInstance().getSite() + "");
        params.put("userAvatar", headicon);
        String userName = CommonUtils.getInstance().getUserName();
        params.put("userName", userName);
        params.put("phone", username);
        params.put("workName", workName);
        params.put("workDescription", workDescription);
        params.put("videoWorkName", videoWorkName);
        params.put("videoWorkDescription", videoDesc);

        model.uploadData(params);
    }


    @Override
    public void uplodError(int code, String msg) {
        view.uplaodError(code, msg);
    }

    @Override
    public void signSuccess() {
        view.signSuccess();
    }

    @Override
    public void checkSign(String actId) {
        model.checkSign(actId);
    }

    @Override
    public void checkError(String msg) {
        view.checkError(msg);
    }

    @Override
    public void checkSuccess() {
        view.checkSuccess();
    }
}
